There is no numerical type which implements both Integral (forced by odd ) and RealFrac (forced by (/) ). (These are typeclasses, if you don't know what I'm talking about, just wait until your tutorial shows about this).
There is no numerical type which implements both Integral (forced by odd) and RealFrac (forced by (/)). (These are typeclasses, if you don't know what I'm talking about, just wait until your tutorial shows about this) You may replace either / by div or do an explicit cast via fromIntegral or similar. You may also do something like x/2 == 1 instead of odd x.
Edit: In your second pastebin file, you did the conversions via truncate, which is also possible. Haskell strength is it's strong typing system, allowing you to do less programming errors but also involves the problem of having strange problem at obvious places. I would generally suggest you to provide type-informations at least at toplevel functions.(like myFunc :: Int -> Integer).
This improves both readability and safety, as the compiler prompts you suddently if something went wrong. In ghci, you can easily find out about type informations via the :t command: ghci> :t odd odd :: (Integral a) => a -> Bool Please notice, that you have to wrap parantheses around infix functions when using this: ghci> :t ($) ($) :: (a -> b) -> a -> b.
1 Thanks FUZxxl. But I still don't understand why should a call isTriangular n "modify" (for lack of a better word) n that makes it unacceptable for odd. – Ninad Sep 4 '10 at 13:57 odd is implemented as odd x = x mod 2 == 1.
The function mod is defined in the typeclass Integral (instances: Integer or Int), whereas / is defined in the typeclass RealFrac (instances Rational', 'Float'). The problem is, that there's no type which is an instance of both Integral` and RealFrac. – FUZxxl Sep 4 '10 at 14:31 I think I was not clear enough about my doubt.My doubt is, why #if isTriangular n then if odd n # should cause a type error and why # if True then if odd n # shouldn't.
Why a call to isTriangular should affect the call odd n? Isn't the whole meaning of a "pure" FPL that a call with same arguments should produce same results always? No function is supposed to have side effects?
– Ninad Sep 4 '10 at 15:13 1 Oh. So it's something like the compiler infers the type of n in series by looking at each place where n is used within series.Am I right? – Ninad Sep 4 '10 at 15:49 1 @Ninad: basically, yes.
More precisely, the type of series is (RealFrac t, Integral t, Integral a) => t -> a, which means that the type of the argument must match both constraints RealFrac (be a fractional number) and Integral (be an integral number). The series function is well-typed because there could be a type that matches both constraints. But when you write series 16, the compiler looks for a type that unifies both constraints (in addition to Num the constraint for integer constants), which doesn't exist (in fact, no type satisfies both constraints).
– Gilles Sep 4 '10 at 22:21.
Because FUZxxI already gave you an answer you were looking for I will show you how your problem can be solved easier and a lot shorter. Just for information. How would you solve your problem in your head?
First, you have to 'generate' sequence containing 1,2,2,3,3,3,4,4,4,4 ... just to know where to change sign. That sequence, expressed in Haskell notation, would be - numbers = concatMap (\x -> replicate x x) 1.. Then you have to 'combine' each number of this sequence with corresponding number from sequence from 1 to n to give it proper sign - that would be - series n = zipWith (\a be -> a*(-1)^(b `mod` 2 + 1)) 1..n numbers Well, and that's it. You have the solution.
You can even express it as one-liner without using numbers variable. But I consider it less readable.
Thanks a ton! That would help a lot! – Ninad Sep 4 '10 at 14:03 This is a very good example of how to think functionally.
+1 – FUZxxl Sep 4 '10 at 14:33.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.